<?php

class Emms_Form_Admin_Role extends HTech_Form
{
    protected $_primary  = 'RoleId';
    
    public function __construct() 
    {
        parent::__construct();        
        
        $this->setName('Role');
                
        $this->setAttrib('title', $this->translate("Role form"));
        $this->setDescription($this->translate("Edit role info or add a new one"));
                
        $roleId = new Zend_Form_Element_Hidden('RoleId');                             
        
        $role = new Zend_Form_Element_Text('Role');
        $role->setLabel("Role")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->addValidator('Db_NoRecordExists',false,
                array(
                    'table'    => 'roles', 
                    'field'    => 'Role', 
                    'exclude'  => array('field' => 'RoleId', 'value' => $this->_id),
                    'messages' => $this->translate("Non valid or duplicated entry")                    
                ))
            ->setDescription(null);
        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
                       
        $alertId = new Zend_Form_Element_Multiselect('LnkAlertsRoles');
        $alertId->setLabel("Alerts")                
            ->setAttrib('size','4')
            ->setRequired(false)
            ->addMultiOptions($this->_db->fetchPairs('select a.AlertId, a.Alert from alerts a'));
        /*
            ->addMultiOptions($this->_db->fetchPairs(sprintf('
                select a.AlertId, a.Alert from alerts a 
                left join lnk_resources_roles r on r.ResourceId = a.ResourceId and r.RoleId = %s 
                where r.RoleId is not null', $this->_id ? $this->_id : (int) $this->_request->getParam('id'))));
         * 
         */
                                
        $resourceId = new Zend_Form_Element_Multiselect('LnkResourcesRoles');
        $resourceId->setLabel("Resources")                
            ->setAttrib('size','20')
            ->setRequired(false)
            ->addMultiOptions($this->_db->fetchPairs('select ResourceId, Resource from view_resources where Public = "No"'));   
                                
        $this->addElements(array($role,$alertId,$resourceId,$comments,$submit,$roleId));
    }
}